package com.lxf.cloudotherserve.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lxf.cloudfeign.client.productClient.ProductClient;
import com.lxf.cloudotherserve.service.BaseStrategy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 罗兴发
 * @data 2025/6/30 17:02
 * @description：产品-策略实现类
 */
@Component("product")
public class ProductStrategy implements BaseStrategy {

    @Resource
    private ProductClient productClient;

    /**
     * 获取产品发布的用户ID
     * @param goodCode
     * @return
     */
    @Override
    public Map getInfos(String goodCode,Integer clrType) {
        //map封装
        Map<String,Object> map = new HashMap<>();
        try {
            JSONObject js = new JSONObject();
            js.put("good_code",goodCode);
            //获取产品详情
            String product = productClient.getProductByCode(js);

            ObjectMapper objectMapper = new ObjectMapper();
            JSONObject jsonObject = objectMapper.readValue(product, JSONObject.class);
            JSONObject productBean = jsonObject.getJSONObject("data");

            //标题
            map.put("objTitle",productBean.getString("goodName"));

            //用户ID
            map.put("userId",productBean.getInteger("userId"));

            //josn封装
            JSONObject j = new JSONObject();
            j.put("good_code",goodCode);

            //获取第一张图片作为封面
            String pi = productClient.getProductImgByCode(j);
            JSONObject jsonObject1 = objectMapper.readValue(pi, JSONObject.class);

            JSONArray list = jsonObject1.getJSONArray("data");
            if (list.size()>0){
                //更新点赞收藏数据量
                for (int i=0;i< list.size();i++) {
                    JSONObject jsonObject2 = list.getJSONObject(i);
                    Integer imgType = jsonObject2.getInteger("imgType");
                    if (imgType==0){
                        map.put("cover",jsonObject2.getString("imgUrl"));
                        break;
                    }
                }
            }else {
                map.put("cover","");
            }
            if (null!=clrType){
                //点赞
                if (clrType==0){
                    Integer likeNum = null;
                    if (null==productBean.getInteger("likeNum")){
                        likeNum = 0;
                    }else {
                        likeNum = productBean.getInteger("likeNum");
                    }
                    likeNum = likeNum + 1;
                    productBean.put("likeNum",likeNum);
                }

                //收藏
                if (clrType==1){
                    Integer collectNum = null;
                    if (null==productBean.getInteger("collectNum")){
                        collectNum = 0;
                    }else {
                        collectNum = productBean.getInteger("collectNum");
                    }
                    collectNum = collectNum + 1;
                    productBean.put("collectNum",collectNum);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
}
